package datasource.image;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.StringTokenizer;

import javax.imageio.ImageIO;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;

public class ImageProcess {
	public static void writeImage(String name, int count) {
		BufferedImage image = null;
		try {
			URL url = new URL(name);
			image = ImageIO.read(url);
			ImageIO.write(image, "jpg", new File("data/" + count + ".jpg"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void processImage(String name, int count) throws IOException {
		URL url = new URL(name);
		BufferedInputStream in = new BufferedInputStream(url.openStream());
		javaxt.io.Image image = new javaxt.io.Image(in);
		double[] gps = image.getGPSCoordinate();
		if (gps != null) {
			System.out.println(count + ":" + Arrays.toString(gps));
		}
		// try {
		// Metadata metadata = ImageMetadataReader.readMetadata(in, false);
		// for (Directory directory : metadata.getDirectories()) {
		// for (Tag tag : directory.getTags()) {
		// System.out.println(tag);
		// }
		// }
		// } catch (ImageProcessingException | IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
	}

	private static String im2url(String imageid, String farm, String server,
			String secret) {
		return String.format("http://farm%s.static.flickr.com/%s/%s_%s.jpg",
				farm, server, imageid, secret);

	}

	private static void processPhoto(String url, String dir, String imageid) {
		BufferedImage image;
		try {
			image = ImageIO.read(new URL(url));
			ImageIO.write(image, "jpg", new File(dir + "/" + imageid + ".jpg"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

//		try {
//			BufferedInputStream in = new BufferedInputStream(
//					new URL(url).openStream());
//			try {
//				Metadata metadata = ImageMetadataReader.readMetadata(in, false);
//				for (Directory directory : metadata.getDirectories()) {
//					for (Tag tag : directory.getTags()) {
//						System.out.println(tag);
//					}
//				}
//				System.out.println();
//			} catch (ImageProcessingException | IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}

	private static void downloadPhoto(String line, String dir, int count) {
		StringTokenizer st = new StringTokenizer(line);
		String imageid = st.nextToken();
		String farm = st.nextToken();
		String server = st.nextToken();
		String secret = st.nextToken();
		System.out.println("Downloading " + count + ":" + imageid);
		String url = im2url(imageid, farm, server, secret);
		processPhoto(url, dir, imageid);

	}

	public static void downloadPhotos(String sourceFile, String dir) {
		try {
			BufferedReader br = new BufferedReader(new FileReader(sourceFile));
			String line = "";
			int count = 0;
			while ((line = br.readLine()) != null) {
				downloadPhoto(line, dir, ++count);
			}
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void processDir(String dir, String output) {
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(output));
			File folder = new File(dir);
			for (File f : folder.listFiles()) {
				String fileName = f.getName();
				if (fileName.endsWith(".jpg")) {
					String result = parseExif(dir + "/" + f.getName());
					String id = fileName
							.substring(0, fileName.lastIndexOf('.'));
					bw.append(id + "\t" + result + "\n");
				}
			}
			bw.flush();
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static String parseExif(String name) {
		try {
			BufferedInputStream in = new BufferedInputStream(
					new FileInputStream(name));

			Metadata metadata = ImageMetadataReader.readMetadata(in, false);
			for (Directory directory : metadata.getDirectories()) {
				for (Tag tag : directory.getTags()) {
					System.out.println(tag);
				}
			}
		} catch (ImageProcessingException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) {
		downloadPhotos(
				"C:\\home\\workspace\\xirong flickr dataset\\MetaDataPlus\\get-images\\id.farm.server.secret.txt",
				"data\\xingron");
	}

}
